package com.koron.order.linepatrol.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.linepatrol.base.bean.po.PatrolPointPo;
import com.koron.order.linepatrol.base.mapper.PatrolPointMapper;
import com.koron.order.linepatrol.base.service.PatrolPointService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡视工单-巡视点从表(PatrolPoint)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-26 15:27:37
 */
@Service("linePatrolPointService")
public class PatrolPointServiceImpl implements PatrolPointService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, PatrolPointPo patrolPoint) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.insertOrUpdate(patrolPoint);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PatrolPointPo> patrolPointList) {

        if (CollectionUtils.isEmpty(patrolPointList)) {
            return 0;
        }

        List<String> patrolIds = patrolPointList.stream().map(PatrolPointPo::getPatrolId).distinct().collect(Collectors.toList());

        Assert.notEmpty(patrolIds, "巡视工单id不能为空");

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        //先根据 巡视工单ids删除
        mapper.deleteByPatrolIds(patrolIds);

        return mapper.insertBatch(patrolPointList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, PatrolPointPo patrolPoint) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.insert(patrolPoint);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<PatrolPointPo> patrolPointList) {
        if (CollectionUtils.isEmpty(patrolPointList)) {
            return 0;
        }
        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.insertOrUpdateBatch(patrolPointList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, PatrolPointPo patrolPoint) {
        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);
        return mapper.update(patrolPoint);
    }

    @Override
    @TaskAnnotation("updateBatchStatus")
    public int updateBatchStatus(SessionFactory factory, List<String> ids, String status, String noPatrolStatus) {
        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);
        return mapper.updateBatchStatus(ids, status, noPatrolStatus);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public PatrolPointPo queryById(SessionFactory factory, String id) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByIds")
    public List<PatrolPointPo> queryByIds(SessionFactory factory, List<String> ids) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.queryByIds(ids);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<PatrolPointPo> queryList(SessionFactory factory, PatrolPointPo patrolPoint) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        List<PatrolPointPo> list = mapper.queryList(patrolPoint);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, PatrolPointPo patrolPoint, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        List<PatrolPointPo> list = mapper.queryList(patrolPoint);

        return new PageInfo(list);
    }

}